package com.gnod.parallaxlistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

/**
 *
 * @author xw.gao
 */
public class ParallaxScollListView extends ListView {

    public final static double NO_ZOOM = 1;
    public final static double ZOOM_X2 = 2;

    private ImageView zoomImageView;
    private ImageView headerImageView;


    private int mDrawableMaxHeight = -1;
    private int mImageViewHeight = -1;
    private int mDefaultImageViewHeight = 0;
	private double mZoomRatio;

    public void setHeaderImageView(ImageView headerImageView) {
        this.headerImageView = headerImageView;
    }

    public void setZoomImageView(ImageView zoomImageView) {
        this.zoomImageView = zoomImageView;
    }


    public ParallaxScollListView(Context context, AttributeSet attrs,
                                 int defStyle) {
        super(context, attrs, defStyle);
    }

    public ParallaxScollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDefaultImageViewHeight = this.getResources().getDimensionPixelSize(R.dimen.size_default_height);

    }

    public ParallaxScollListView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == ev.ACTION_UP)
        {

           ResetAnimation anim = new ResetAnimation(mDefaultImageViewHeight);
            anim.setDuration(300);
           zoomImageView.startAnimation(anim);

        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
                                   int scrollY, int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {

        if(deltaY < 0) //下拉
        {

            zoomImageView.getLayoutParams().height = zoomImageView.getHeight() - deltaY;
            zoomImageView.requestLayout();
        }
        else  //上拉
        {
            View header = (View) zoomImageView.getParent();
            if(header.getHeight() > mDefaultImageViewHeight)
            {
                zoomImageView.getLayoutParams().height = zoomImageView.getHeight() - deltaY;
                header.layout(header.getLeft(),0,header.getRight(),header.getHeight());
                zoomImageView.requestLayout();
            }

        }

     return  super.overScrollBy( deltaX,  deltaY,  scrollX,
         scrollY,  scrollRangeX,  scrollRangeY,
         maxOverScrollX,  maxOverScrollY,  isTouchEvent);

    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {


        View header = (View) zoomImageView.getParent();

        if(header.getTop()<0 && (zoomImageView.getHeight() > mDefaultImageViewHeight)) //上滑
        {

            zoomImageView.getLayoutParams().height = zoomImageView.getHeight() + header.getTop();
            header.layout(header.getLeft(),0,header.getRight(),header.getHeight());
            zoomImageView.requestLayout();


        }
        super.onScrollChanged(l, t, oldl, oldt);




    }

    public class ResetAnimation extends  Animation
    {
        public int mOriginHeight;
        public int mTargetHeight;
        public int mExtraHeight;

        public ResetAnimation(int targetHeight)
        {
            mOriginHeight = zoomImageView.getHeight();
            mTargetHeight = targetHeight;
            mExtraHeight = mOriginHeight - mTargetHeight;


        }
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {

            zoomImageView.getLayoutParams().height = (int)( mOriginHeight - mExtraHeight*interpolatedTime);
            zoomImageView.requestLayout();
            super.applyTransformation(interpolatedTime, t);
        }
    }


}
